#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>

#include "../comm/httplib.h"
#include "../comm/util.hpp"
#include "main_server_model.hpp"
#include "main_server_view.hpp"

namespace ns_control
{
    using namespace ns_model;
    using namespace ns_view;
    using namespace httplib;

    class control
    {
    private:
        model _model;
        view _view;

    public:
        control()
        {
        }
        ~control()
        {
        }

    public:
        // 获取首页
        bool getIndex(std::string *html)
        {
            bool ret = true;
            std::unordered_map<std::string, std::vector<struct book>> hotBooks;
            std::vector<struct book> newBooks;
            if (_model.getHotBooks(&hotBooks) && _model.getNewBooks(&newBooks))
            {
                _view.indexExpandHtml(hotBooks, newBooks, html);
            }
            else
            {
                if (!ns_util::fileUtil::readFile("./template_html/unaccess.html", html))
                {
                    LOG(FATAL) << "读取ERRORHTML失败"
                               << "\n";
                }
                ret = false;
            }
            return ret;
        }

        // 获取全部作品
        bool allBooks(const std::string &page, std::string *html)
        {
            bool ret = true;
            std::vector<struct book> all;
            if (_model.getAllBooks(page, &all))
            {
                sort(all.begin(), all.end(), [](const struct book &b1, const struct book &b2)
                     { return atoi(b1._book_id.c_str()) < atoi(b2._book_id.c_str()); });
                _view.allExpandHtml(all, page, html, "all");
            }
            else
            {
                if (!ns_util::fileUtil::readFile("./template_html/unaccess.html", html))
                {
                    LOG(FATAL) << "读取ERRORHTML失败"
                               << "\n";
                }
                ret = false;
            }
            return ret;
        }

        // 获取分类作品
        bool typeOfBooks(const std::string &type, const std::string &page, std::string *html)
        {
            bool ret = true;
            std::vector<struct book> all;
            if (_model.getTypeOfBooks(type, page, &all))
            {
                sort(all.begin(), all.end(), [](const struct book &b1, const struct book &b2)
                     { return atoi(b1._book_id.c_str()) < atoi(b2._book_id.c_str()); });
                _view.allExpandHtml(all, page, html, type);
            }
            else
            {
                if (!ns_util::fileUtil::readFile("./template_html/unaccess.html", html))
                {
                    LOG(FATAL) << "读取ERRORHTML失败"
                               << "\n";
                }
                ret = false;
            }
            return ret;
        }

        // 登录
        bool login(std::string *html)
        {
            bool ret = true;
            if (!ns_util::fileUtil::readFile("./template_html/login.html", html))
            {
                if (!ns_util::fileUtil::readFile("./template_html/unaccess.html", html))
                {
                    LOG(FATAL) << "读取ERRORHTML失败"
                               << "\n";
                    ret = false;
                }
                LOG(FATAL) << "读取LOGINHTML失败"
                           << "\n";
            }
            return ret;
        }

        // 登录参数提交
        void loginSumbit(const std::string &body, std::string *result)
        {
            _model.loginSumbit(body, result);
        }

        // 判断登录是否成功
        bool loginSuccess(const std::string &userid, std::string *html)
        {
            bool ret = true;
            struct user usr;
            if (_model.getUserInfo(userid, html, &usr))
            {
                _view.userExpandHtml(usr, html);
            }
            else
            {
                if (!ns_util::fileUtil::readFile("../template_html/unaccess.html", html))
                {
                    LOG(FATAL) << "读取ERRORHTML失败"
                               << "\n";
                }
                ret = false;
            }
            return ret;
        }

        // 用户注册
        bool registerUser(const std::string &in_json, std::string *out_json)
        {
            bool ret = true;
            struct user usr;
            if (_model.addUser(in_json, out_json))
            {
                return true;
            }
            return false;
        }

        // 差错处理
        void none(std::string *html)
        {
            if (!ns_util::fileUtil::readFile("./template_html/unaccess.html", html))
            {
                LOG(FATAL) << "读取ERRORHTML失败"
                           << "\n";
            }
        }
    };
}